package com.example.peibei.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.RadioButton;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.dingtao.common.bean.PaiDanDetail;
import com.dingtao.common.bean.SubmitOrder;
import com.example.peibei.R;

import java.util.List;

public class PaidanDateAdapter extends BaseQuickAdapter<PaiDanDetail.SelectorDTO, BaseViewHolder> {
    private int mSelectedItem = -1;
    private Context mContext;

    public PaidanDateAdapter(@Nullable List<PaiDanDetail.SelectorDTO> data, Context mContext) {
        super(R.layout.item_radiobutton, data);
        this.mContext = mContext;
    }

    @SuppressLint({"ResourceAsColor", "UseCompatLoadingForColorStateLists"})
    @Override
    protected void convert(BaseViewHolder helper, PaiDanDetail.SelectorDTO item) {
        String realDate = item.getRealDate();
        String substring = realDate.substring(5, 11);
        helper.setText(R.id.tv_date,substring);
        RadioButton bt_week = helper.getView(R.id.bt_week);
        bt_week.setText(item.getTips());
        bt_week.setChecked(helper.getLayoutPosition() == mSelectedItem);
        bt_week.setTag(helper.getLayoutPosition());
        if (item.getStatus() == 0) {
            bt_week.setBackgroundResource(R.drawable.shape_radiobutton_close);
            bt_week.setTextColor(mContext.getResources().getColorStateList(R.color.gray_home_tran));
            bt_week.setClickable(false);
            helper.setTextColor(R.id.tv_date, mContext.getResources().getColor(R.color.gray_home_tran));
        } else {
            helper.setBackgroundResource(R.id.bt_week, R.drawable.select_radiobutton);
            if(bt_week.isChecked()){
                helper.setTextColor(R.id.tv_date, mContext.getResources().getColor(R.color.color_main));
                bt_week.setTextColor(mContext.getResources().getColorStateList(R.color.white));
            }else{
                helper.setTextColor(R.id.tv_date, mContext.getResources().getColor(R.color.text_dark_gray));
                bt_week.setTextColor(mContext.getResources().getColorStateList(R.color.gray_home));
            }
            bt_week.setClickable(true);
        }
        bt_week.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.getStatus() == 0) {

                } else {
                    mSelectedItem = (int) v.getTag();
                    notifyDataSetChanged();
                    if (mListener != null) {
                        mListener.onItemClick(item, helper.getLayoutPosition());
                    }
                }
            }
        });
    }

    public interface OnItemClickListener {
        void onItemClick(PaiDanDetail.SelectorDTO data, int pos);
    }

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }
}
